//
//  HaomalanVC.m
//  SSC
//
//  Created by 红沙尘 on 17/6/18.
//  Copyright © 2017年 红沙尘. All rights reserved.
//

#import "HaomalanVC.h"
#import "HaomalanCell.h"
#import "WanfaModel.h"
#import "ZhuihaoView.h"
#import "AppDelegate.h"
#import "WanfaManager.h"

@interface HaomalanVC ()<HaomalanCellDelegate>
{
    __weak IBOutlet NSLayoutConstraint *lineA;
    __weak IBOutlet NSLayoutConstraint *lineB;
    __weak IBOutlet NSLayoutConstraint *bottomH;
    
    __weak IBOutlet UILabel *hhLabel;
    __weak IBOutlet UILabel *mmLabel;
    __weak IBOutlet UILabel *ssLabel;
    __weak IBOutlet UILabel *qiLabel;

    __weak IBOutlet UILabel *moneyLabel;
    __weak IBOutlet ZhuihaoView *zhuihaoView;
    
    CGFloat payMoney;
}

@property (nonatomic, strong) HaomalanManager *lanziManager;
@property (nonatomic, copy) NSString *qihao;
@property (nonatomic, assign) CaipiaoType otype;

@end

@implementation HaomalanVC

- (id)initWithLanzi:(HaomalanManager *)lanzi qi:(NSString *)qi otype:(CaipiaoType)otype {
    
    self = [super init];
    
    if (self) {
        self.lanziManager = lanzi;
        self.qihao = qi;
        self.otype = otype;
    }
    
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTitle:@"号码篮"];
    
    NSString *type = @"";
    
    switch (self.otype) {
        case CaipiaoTypeShishicai:
        {
            type = @"重庆时时彩";
        }
            break;
        case CaipiaoTypeFenfencai:
        {
            type = @"两分彩";
        }
            break;
        default:
            break;
    }
    
    UIButton *rightBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 90, 44)];
    [rightBtn setTitle:type forState:UIControlStateNormal];
    rightBtn.titleLabel.font = [MTool fontOfSize:14];
    
    self.navigationItem.rightBarButtonItem = CUSTOMVIEW(rightBtn);
    
    qiLabel.text = self.qihao;
    zhuihaoView.otype = self.otype;
}

- (IBAction)deleteAll {
    [self.lanziManager removeAllOjbect];
    
    if ([self.lanziManager.lanziArray count] == 0) {
        [self.navigationController popViewControllerAnimated:YES];
        return;
    }
    
    [mainTableView reloadData];
}

- (IBAction)xiazhu {
    
    WeakSelf
    
    NSString *msg = [NSString stringWithFormat:@"\n确定加入：%@期\n总金额：%.2f元",qiLabel.text,payMoney];
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"确定投注信息"
                                                                             message:msg
                                                                      preferredStyle:UIAlertControllerStyleAlert];
    
    [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
    [alertController addAction:[UIAlertAction actionWithTitle:@"确定"
                                                        style:UIAlertActionStyleDestructive
                                                      handler:^(UIAlertAction * _Nonnull action)
                                {
                                    [weakSelf goPay];
                                }]];
    
    [self presentViewController:alertController animated:YES completion:nil];
}

- (void)goPay {
    
    NSMutableArray *list = @[].mutableCopy;
    
    for (int i = 0; i < self.lanziManager.lanziArray.count; i ++) {
        
        WanfaModel *model = self.lanziManager.lanziArray[i];
        
        NSMutableArray *array = [WanfaManager haomaWithModel:model beishu:nil];
        if (array.count > 0) {
            [list addObjectsFromArray:array];
        }
    }
    
    payMoney   = 0;
    int zhushu = 0;
    
    for (WanfaModel *model in self.lanziManager.lanziArray) {
        payMoney  += model.payMoney;
        zhushu += model.zhushu;
    }
    
    [LoadingView show];
    
    [SSCRequest bettingWithAmount:[NSString stringWithFormat:@"%f",payMoney]
                            issue:qiLabel.text
                          numbers:[NSString stringWithFormat:@"%d",zhushu]
                            otype:self.otype
                             list:list
                          success:^(NSDictionary *responseObject) {
                              [LoadingView diss];
                              [MTool showMessage:responseObject[@"msg"] inView:nil];
                          } fail:^(NSError *error) {
                              [LoadingView diss];
                          }];
}

- (IBAction)payZhuihao {
    
    NSMutableArray *list = @[].mutableCopy;
    
    for (int i = 0; i < self.lanziManager.lanziArray.count; i ++) {
        
        WanfaModel *model = self.lanziManager.lanziArray[i];
        
        NSMutableArray *array = [WanfaManager haomaWithModel:model beishu:zhuihaoView.beishu];
        
        if (array.count > 0) {
            [list addObjectsFromArray:array];
        }
    }
    
    int payAmount = 0;
    
    for (int i = 0; i < list.count; i ++) {

        NSMutableDictionary *dict = list[i];
        dict[@"doubles"] = zhuihaoView.beishu;
        payAmount += [dict[@"amount"] intValue];
    }
    
    [LoadingView show];
    
    [SSCRequest planBettingWithIssuenum:[NSString stringWithFormat:@"%d",(int)zhuihaoView.qiCount]
                                 isstop:[NSString stringWithFormat:@"%d",(int)zhuihaoView.selected]
                                 amount:[NSString stringWithFormat:@"%d",payAmount]
                                  issue:zhuihaoView.startIssue
                                numbers:zhuihaoView.zhushu
                                  otype:self.otype
                                   list:list
                                success:^(NSDictionary *responseObject)
    {
        [LoadingView diss];
        [MTool showMessage:responseObject[@"msg"] inView:nil];
    } fail:^(NSError *error) {
        [LoadingView diss];
    }];
}

- (IBAction)typeChanged:(UISegmentedControl *)segment {
    
    if (segment.selectedSegmentIndex == 0) {
        zhuihaoView.hidden = YES;
    } else {
        zhuihaoView.hidden = NO;
        
        int zhushu = 0;
        
        for (WanfaModel *model in self.lanziManager.lanziArray) {
            zhushu += model.zhushu;
        }
        
        zhuihaoView.zhushu = [NSString stringWithFormat:@"%d",zhushu];
//        zhuihaoView.money = [NSString stringWithFormat:@"%d",(int)payMoney];
    }
}

- (void)deleteBtnPressed:(UIButton *)button {
    [self.lanziManager removeObjectAtIndex:button.tag];
    
    if ([self.lanziManager.lanziArray count] == 0) {
        [self.navigationController popViewControllerAnimated:YES];
        return;
    }
    
    [mainTableView reloadData];
}

- (void)updateQi:(NSString *)qi hh:(NSString *)hh mm:(NSString *)mm ss:(NSString *)ss {
    
    qi = [qi stringByReplacingOccurrencesOfString:@"第" withString:@""];
    qi = [qi stringByReplacingOccurrencesOfString:@"期" withString:@""];
    
    qiLabel.text = qi;
    
    hhLabel.text = hh;
    mmLabel.text = mm;
    ssLabel.text = ss;
}

#pragma mark - HaomalanCellDelegate

- (void)payMoneyDidChanged {
    
    payMoney   = 0;
    int zhushu = 0;
    
    for (WanfaModel *model in self.lanziManager.lanziArray) {
        payMoney  += model.payMoney;
        zhushu += model.zhushu;
    }
    
    moneyLabel.text = [NSString stringWithFormat:@"共 %d 注  投资总额 %.2f 元",zhushu,payMoney];
//    zhuihaoView.money = [NSString stringWithFormat:@"%d",(int)payMoney];
    //改变追号页面支付的金额
}

#pragma mark - tableview

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 12;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    bottomH.constant = self.lanziManager.lanziArray.count == 0 ? 0 : 40;
    
    return [self.lanziManager.lanziArray count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HaomalanCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HaomalanCell"];
    cell.deleteBtn.tag = indexPath.section;
    cell.hDelegate = self;
    [cell.deleteBtn addTarget:self action:@selector(deleteBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
    
    
    [cell loadWithModel:self.lanziManager.lanziArray[indexPath.section]];
    
    return cell;
}

#pragma mark - setup

- (void)setupData {
    AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
    app.lanziVC = self;
    [self updateQi:app.qi hh:app.hh mm:app.mm ss:app.ss];
}

- (void)setupUI {
    lineA.constant = LineHeight;
    lineB.constant = LineHeight;
    
    hhLabel.layer.cornerRadius  = 2;
    hhLabel.layer.masksToBounds = YES;
    mmLabel.layer.cornerRadius  = 2;
    mmLabel.layer.masksToBounds = YES;
    ssLabel.layer.cornerRadius  = 2;
    ssLabel.layer.masksToBounds = YES;
    
    zhuihaoView.hidden = YES;
    
    [mainTableView registerNib:[UINib nibWithNibName:@"HaomalanCell" bundle:nil]
        forCellReuseIdentifier:@"HaomalanCell"];
    mainTableView.rowHeight = UITableViewAutomaticDimension;
    mainTableView.estimatedRowHeight = 120;
    
    bottomH.constant = self.lanziManager.lanziArray.count == 0 ? 0 : 40;
}

@end
